package com.mundus.game;


import javax.swing.JFrame;
import javax.swing.JButton;

import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.DefaultComboBoxModel;

import com.mundus.engine.Settings;
import com.mundus.engine.Settings.Resolution;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFormattedTextField;

@SuppressWarnings({ "unchecked", "rawtypes", "serial" })
public class GameLauncher extends JFrame {
	/**
	 * Create the frame.
	 */
	JComboBox comboBoxResolution;
	JCheckBox chckbxFullscreen;
	JCheckBox chckbxVsync;
	JFormattedTextField formattedTextFieldFrameLimit;
	JCheckBox chckbxDoNotShow;
	
	public GameLauncher() {		
		Settings.initSettings();
		setTitle(Settings.getTitle());	
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 430, 183);
		getContentPane().setLayout(null);
		
		initJComponents();		
	}
	
	private void initJComponents(){
		
		//Save and launch button
		JButton btnSaveAndLaunch = new JButton("Save and Launch");
		btnSaveAndLaunch.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent paramMouseEvent) {
				launchGame();
			}
		});
		btnSaveAndLaunch.setBounds(249, 111, 149, 23);
		getContentPane().add(btnSaveAndLaunch);
		
		//Exit button
		JButton btnExit = new JButton("Exit");
		btnExit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				System.exit(0);
			}
		});
		btnExit.setBounds(10, 111, 89, 23);
		getContentPane().add(btnExit);
		
		//Save button
		JButton btnSave = new JButton("Save");
		btnSave.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				saveSettings();
			}
		});
		btnSave.setBounds(109, 111, 89, 23);
		getContentPane().add(btnSave);		
		
		//Content Panel
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(Color.LIGHT_GRAY));
		panel.setBounds(10, 11, 388, 69);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		//Resolution label
		JLabel lblNewLabel = new JLabel("Resolution:");
		lblNewLabel.setBounds(24, 11, 63, 14);
		panel.add(lblNewLabel);
		
		//Resolution combo Box
		comboBoxResolution = new JComboBox();
		comboBoxResolution.setModel(new DefaultComboBoxModel(Resolution.getResolutionValues()));
		comboBoxResolution.setBounds(97, 8, 281, 20);
		comboBoxResolution.setSelectedItem(Settings.getResolution().getStringLiteral());
		panel.add(comboBoxResolution);
		
		//Fullscreen tick box
		chckbxFullscreen = new JCheckBox("Fullscreen");
		chckbxFullscreen.setBounds(24, 39, 97, 23);
		chckbxFullscreen.setSelected(Settings.getFullscreen());
		panel.add(chckbxFullscreen);
		
		//VSync tick box
		chckbxVsync = new JCheckBox("VSync");
		chckbxVsync.setBounds(123, 39, 63, 23);
		chckbxVsync.setSelected(Settings.getVsync());
		panel.add(chckbxVsync);
		
		//FPS limit num box
		formattedTextFieldFrameLimit = new JFormattedTextField();
		formattedTextFieldFrameLimit.setBounds(299, 40, 79, 20);
		formattedTextFieldFrameLimit.setValue(Settings.getFPSLimit());
		panel.add(formattedTextFieldFrameLimit);
		
		//FPS limit label
		JLabel lblFrameLimit = new JLabel("FPS Limit:");
		lblFrameLimit.setBounds(218, 43, 71, 14);
		panel.add(lblFrameLimit);
		
		//Show on start checkbox
		chckbxDoNotShow = new JCheckBox("Show launcher");
		chckbxDoNotShow.setBounds(10, 87, 135, 23);
		chckbxDoNotShow.setSelected(Settings.DISPLAYLAUNCHER);
		getContentPane().add(chckbxDoNotShow);
	}
	
	public void showLauncher(){
		if (Settings.DISPLAYLAUNCHER)
			setVisible(true);
		else
			launchGame();
	}
	
	private void saveSettings(){
		Settings.updateSettings(comboBoxResolution.getSelectedItem().toString(), chckbxVsync.isSelected(), chckbxFullscreen.isSelected(),
				Integer.parseInt(formattedTextFieldFrameLimit.getValue().toString()),chckbxDoNotShow.isSelected());
	}
	
	private void launchGame(){
		setVisible(false);
		saveSettings();
		dispose();
		
		Game game = new Game();
		game.run();
	}
}
